package com.ruoyi.tea.api;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tea.cache.SeeCache;
import com.ruoyi.tea.domain.TeaRoom;
import com.ruoyi.tea.domain.TeaSeeLive;
import com.ruoyi.tea.domain.TeaUser;
import com.ruoyi.tea.service.ITeaRoomService;
import com.ruoyi.tea.service.ITeaSeeLiveService;
import com.ruoyi.tea.service.ITeaUserService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 直播接口
 *
 * @author wangfeng
 * @time 2022年08月05日23:46:14
 */
//TODO 该位置需要考虑查库、插库、更新导致问题锁表问题
@Controller
@RequestMapping("/wx/api/room")
public class LiveApiController extends BaseController {

    @Autowired
    private ITeaUserService iTeaUserService;

    @Autowired
    private ITeaSeeLiveService iTeaSeeLiveService;

    @Autowired
    private ITeaRoomService iTeaRoomService;

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list( @RequestBody TeaRoom teaRoom) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaRoom.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(500);
            tableDataInfo.setMsg("未登录");
            return tableDataInfo;
        }
        startPage();
        List<TeaRoom> list = iTeaRoomService.selectTeaRoomListApi(teaRoom);
        return getDataTable(list);
    }

    /**
     * 保存录播观看记录
     *
     * @param teaSeeLive
     * @return
     */
    @RequestMapping("/see/history/save")
    @ResponseBody
    public AjaxResult seeLiveHistory(@RequestBody TeaSeeLive teaSeeLive) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaSeeLive.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            return AjaxResult.error("未登录");
        }
        teaSeeLive.setUserName(teaUserList.get(0).getName());
        teaSeeLive.setNickName(teaUserList.get(0).getNickName());
        teaSeeLive.setPhone(teaUserList.get(0).getPhone());
        teaSeeLive.setUserCode(teaUserList.get(0).getUserCode());
        teaSeeLive.setStartTime(new Date());
        teaSeeLive.setEndTime(null);
        iTeaSeeLiveService.insertTeaSeeLive(teaSeeLive);
        SeeCache.SEE_HSITORY_CACHE.put(teaSeeLive.getUserName() + "live", System.currentTimeMillis() / 1000);
        return AjaxResult.success();
    }

    /**
     * 心跳进行结束时间更新
     * 没10秒更新一次
     *
     * @param teaSeeLive
     * @return
     */
    @RequestMapping("/see/history/heartbeat")
    @ResponseBody
    public AjaxResult seeLiveHeartbeat(@RequestBody TeaSeeLive teaSeeLive) {
        TeaSeeLive select = new TeaSeeLive();
        select.setOpenId(teaSeeLive.getOpenId());
        select.setRoomCode(teaSeeLive.getRoomCode());
        List<TeaSeeLive> teaSeeLives = iTeaSeeLiveService.selectTeaSeeLiveList(select);
        if (teaSeeLives != null && teaSeeLives.size() != 0) {
            TeaSeeLive update = new TeaSeeLive();
            update.setId(teaSeeLives.get(teaSeeLives.size()-1).getId());
            update.setEndTime(new Date());
            iTeaSeeLiveService.updateTeaSeeLive(update);
        }
        return AjaxResult.success();
    }

    @RequestMapping("/info")
    @ResponseBody
    public AjaxResult info(@RequestBody TeaRoom teaRoom){
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaRoom.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            return AjaxResult.error("未登录");
        }
        return AjaxResult.success(iTeaRoomService.selectTeaRoomById(teaRoom.getId()));
    }
}
